package com.huaao.micropolice.core.service.content.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.common.page.Sort;
import com.huaao.micropolice.core.model.content.Subject;
import com.huaao.micropolice.core.repository.content.SubjectDao;
import com.huaao.micropolice.core.service.content.SubjectService;

@Service
public class SubjectServiceImpl implements SubjectService {

	@Autowired
	private SubjectDao subjectDao;
	@SuppressWarnings("unchecked")
	public List<Subject> listSubject(Page page){
		
		return subjectDao.findByPage(Subject.class, null, new Sort("sort", "asc"), page);
	}
	
	@Transactional
	public String  addSubject(Subject newSubject){
		if(newSubject.getDept()!=null&&StringUtils.isBlank(newSubject.getDept().getId())){
			newSubject.setDept(null);
		}
		
		if(newSubject.getParent()!=null&&StringUtils.isBlank(newSubject.getParent().getId())){
			newSubject.setParent(null);
		}
		return subjectDao.save(newSubject);
	}
	
	@Transactional
	public void updateSubject(Subject subject){
		
		subjectDao.update(subject);
	}
	
	@Transactional(readOnly=false)
	public void deleteSubject(String id){
		
//		String likeStr = "%,"+id+",%";
//		subjectDao.deleteChildPidsLike(likeStr);
		subjectDao.deleteById(Subject.class,id);
	}

	@Override
	public Subject get(String subjectId) {
		// TODO Auto-generated method stub
		return (Subject) subjectDao.get(Subject.class, subjectId);
	}
	@Override
	public boolean existSubjectBy(String subjectName) {
		// TODO Auto-generated method stub
		return subjectDao.existSubjectBy(subjectName);
	}

	@Override
	public List<Subject> findByDept(String deptId) {
		return subjectDao.findByDept(deptId);
	}



}
